<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="85px"
    >
      <el-form-item label="关键词" prop="keyword">
        <ns-input
          v-model="queryParams.keyword"
          class="!w-240px"
          clearable
          placeholder="请输入关键词"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键词组" prop="keywordType">
        <el-cascader
          style="width: 100%"
          v-model.trim="queryParams.keywordType"
          :options="ketWordTree"
          :props="cascaderProps"
          clearable
          placeholder="请选择关键词组"
          maxlength="50"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" v-hasPermi="['develop:featureLibrary:search']" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" v-hasPermi="['develop:featureLibrary:search']" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <div class="handle-btns">
      <el-button
        type="primary"
        @click="openForm('create')"
        v-hasPermi="['develop:featureLibrary:add']"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="list" row-key="id" show-overflow-tooltip border>
      <el-table-column type="index" width="80" label="序号" align="center" />
      <el-table-column prop="keyword" label="关键词" align="center" />
      <el-table-column prop="keywordType" width="250" label="关键词组" align="center" />
      <el-table-column prop="keywordType" width="250" label="结构化类型" align="center">
        <template #default="scope">
          {{ scope.row.type == 0 ? '非结构化数据' : '结构化数据' }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="150" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['develop:featureLibrary:delete']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['develop:featureLibrary:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.page"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <KeywordForm ref="formRef" :types="keywordTypeList" @success="successHandle" />
</template>

<script setup lang="ts">
import { Plus } from '@element-plus/icons-vue'
import {
  getKeywordPage,
  deleteKeyword,
  getKeywordType as getKeywordTypeRemote
} from '@/api/develop/featureLibrary/keyword'
import { getKeywordTree } from '@/api/develop/featureLibrary/keyword'
import type { FormType } from './formDialog/KeywordForm.vue'
import KeywordForm from './formDialog/KeywordForm.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const queryFormRef = ref() // 搜索的表单
const total = ref(10) // 列表的总页数
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const ketWordTree = ref([])
const keywordTypeList = ref<string[]>([])
const queryParams = reactive({
  page: 1,
  pageSize: 10,
  keywordType: '',
  keyword: ''
})

const cascaderProps = ref({
  checkStrictly: true,
  label: 'keywordType',
  value: 'keywordType',
  children: 'children',
  emitPath: false
})
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.page = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 暴露方法 */
defineExpose({
  resetQuery
})

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof KeywordForm>>()
const openForm = (type: FormType, id?: number) => {
  formRef.value?.open(type, id)
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const { count, data } = await getKeywordPage(toRaw(queryParams))
    list.value = data
    total.value = count
  } finally {
    loading.value = false
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await deleteKeyword(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

async function getKeywordType() {
  const list = await getKeywordTypeRemote()
  keywordTypeList.value = list
}

const getKeyWordTree = async () => {
  const data = await getKeywordTree()
  ketWordTree.value = data || []
}

function successHandle() {
  getList()
  getKeywordType()
}

onMounted(() => {
  getList()
  getKeywordType()
  getKeyWordTree()
})
</script>

<style lang="scss" scoped>
.handle-btns {
  margin-bottom: 20px;
}
</style>
